var conditionSetting = {
	init : function() {
		conditionSetting.alarmId = '';
		$('.tab-alarm').height(nav.whiceH[1]);
		this.queryTabInit = this.quaryTab();//this.queryTabInit.reflash(0);
		this.updateInit = this.saveVehicleTired();
		this.choseCar();
		this.annal();
		this.allVehicleId="";
		this.allVehicleHtml=""
		
		this.initEvent();
	this.loadUserVehicle();
		
	},loadUserVehicle :function(keyWord){
		
//加载用户拥有的所有车辆
		
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleTired/loadUserVehicle.html",
			cache : false,
			dataType : "JSON",
			data:{"analyseID":2,"keyWord":keyWord},
			success : function(data) {
				if(data.vehicleList &&  data.vehicleList.length>0){
				var arr = data.vehicleList;
				var size = arr.length;
				conditionSetting.vehicleArr=arr;
				conditionSetting.vehicleSize=size;
				conditionSetting.allVehicleId="";
				var html="";
				for (var i = 0; i < size; i++) {
					if (i == 0) {
						conditionSetting.allVehicleId+=arr[i].vehicleId+",";
						html+='<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>';
					} else {
						conditionSetting.allVehicleId+=arr[i].vehicleId+",";;
						html+='<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>';
					}
				}
				conditionSetting.allVehicleHtml=html;
				$(".queryCarCont").html(html);

			}
			}
		});
		
		
		
	},
	initEvent : function() {
		
	//车辆选择方式的控制(默认全选)
		 $("#allVehicle").on("click",function(){
	      	  var obj = document.getElementById("vehicleTab");
	            obj.style.display= "none";
	      })
	     $("#chooseVehicle").on("click",function(){
	      	  var obj = document.getElementById("vehicleTab");
	      	  obj.style.display= "block";
	     })
		
    
		//关键字查车辆
		$("#vehicleSearch").click(function() {
			 var ids = $(".resultCarCont li div").text(); 
			 var value = $("#queryValue").val();
			 
			 conditionSetting.loadUserVehicle(value);

			
			
		})
		$(".queryCarCont ").on("click", "li", function() {

			$(this).toggleClass("active");
		})
		$(".queryCarCont").on("dblclick","li",function() {
			var _this=this;
			$(this).remove();
			//$(this).addClass("active");
			$(".resultCarCont").append(this);

		})

		$(".resultCarCont").on("click", "li", function() {
			$(this).toggleClass("active");
		})
		$(".resultCarCont ").on("dblclick", "li", function() {
			var _this=this
			$(_this).remove();
			$(".queryCarCont").prepend(_this);

		})
		$(".go").click(function() {
			$(".resultCarCont").append($(".queryCarCont .active"));
			$(".queryCarCont .active").remove();
		})
		$(".back").click(function() {
			$(".queryCarCont").prepend($(".resultCarCont .active"));
			$(".resultCarCont .active").remove();
		})
		
		/* $("#coverageGroupsName").select2();//启动select2
		
		$.ajax({
        	url : appCtx + 'reportset/vehicleTired/queryCoverageGroups.html',
            dataType: 'json',//数据类型
            width:300,
            success:function(data){
            	var data=data.data;
            	var size=data.length;
            	var selectStr="";
            	selectStr+="<option value=''>请选择分析地点</option>";
            	if(size>0){
            		for (var i = 0; i < size; i++) {
            			selectStr+="<option value='"+data[i].ID+"'>"+data[i].name+"</option>";
					}
            		$("#coverageGroupsName").html(selectStr);
            	}
            }
		})*/
		$("#saveOrUpdateTab").click(function(){
			conditionSetting.alarmId = '';
			 $(".queryCarCont").html("");
			 $("#queryValue").val("");
			 conditionSetting.loadUserVehicle();
			 $(".resultCarCont li div").text("");
			 $("#name").val("");//分析组名
			 
			 $("#continuousRestTime").val("");//休息时间
			 $("#timeThreshold").val("");//连续驾驶时间
			 $("#earlyWarningThreshold").val("");//疲劳预警值
			 $("#remindInterval").val("");//提醒间隔
			 $("#remindType2").attr("checked","checked");//报警
			 $("#commandContent").val("");//播报内容
			 $("#earlyCmdContent").val("");
			 $("#wechart").attr("checked",false);
			 $("#notify").attr("checked",false);
			 $("#remark").val("");
			 $("#allVehicle").attr("checked","checked");
			 $(".resultCarCont li").remove();
			 var obj = document.getElementById("vehicleTab");
             obj.style.display= "none";
		})
		
		$("#validateBtn").click(function(){
	$("velidateModal").modal("hide");
})
 $('#velidateModal').on('hidden', function() {
$("#content p").remove();
		})

	},

	quaryTab : function() {
		var index = 0;
		var _this = this;
		var func = function(){
			$('.tab-alarm-cont-list').hide().eq(index).show();
			$(this).addClass('active');
			$(this).siblings().removeClass('active');
			$('.date-alarm').find('.line').css({
				'left' : 100 * index
			});
		};
		$('.date-alarm').find('.list').click(function() {
			index = $(this).index();
			func();
		});
		return {
			'index' : index,
			reflash : function(i){
				index = i;
				func();
			}
		}
	},

	/**
	 * 车辆选择
	 */
	choseCar : function() {
		$('#selectCar').select2();
		
	},
	queryOtherVehicle:function(analyseGroupsId){
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/queryOtherVehicle.html",
			cache : false,
			dataType : "JSON",
			 data:{"analyseGroupsId":analyseGroupsId,"analyseID":2},
			success : function(data) {
				console.log(data);
				 
				var arrOtherVehicle = data.data;
				var sizeOtherVehicle = arrOtherVehicle.length;
				var html="";
				for (var i = 0; i < sizeOtherVehicle; i++) {
					if (i == 0) {
						
						html+='<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arrOtherVehicle[i].VNAME
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arrOtherVehicle[i].VID + ',</div></li>'
					} else {
						html+='<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arrOtherVehicle[i].VNAME
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arrOtherVehicle[i].VID + ',</div></li>'
					}
				}
				$(".queryCarCont").html(html);
			}
		});
	},
	updateVehicleSpeed : function(id,analyseGroupsID) {
		conditionSetting.queryTabInit.reflash(1);
		conditionSetting.alarmId = id;
		$(".queryCarCont").html("");
		conditionSetting.loadUserVehicle();
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + 'reportset/vehicleSpeed/queryVehicle.html',
			cache : false,
			data : {"id":analyseGroupsID},
			dataType : "JSON",
			success : function(data) {
				var arr=data.rows;
				var size=arr.length;
				var vehicleArr=conditionSetting.vehicleArr
				var vehicleSize=conditionSetting.vehicleSize
				$(".resultCarCont li").remove();
					
				for (var i = 0; i < size; i++) {
					if (i == 0) {
						$(".resultCarCont").append(
								'<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>')
					} else {
						$(".resultCarCont").append(
								'<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>')
					}
				}
				
			}
			

		});
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleTired/updateVehicleTired.html",
			cache : false,
			data : {"id":id},
			dataType : "JSON",
			success : function(data) {
				
				var data=data.data;
				$("#name").val(data.name);
				$("#remark").val(data.remark);
				
				var continuousRestTime=(data.continuousRestTime)/60;
				   var anum=continuousRestTime.toString().indexOf(".")
					if(anum>0){
						continuousRestTime=continuousRestTime.toFixed(2);
				}
				$("#continuousRestTime").val(continuousRestTime);//休息时间
				
				var timeThreshold=(data.timeThreshold)/60;
				
				   var bnum=timeThreshold.toString().indexOf(".")
					if(bnum>0){
						timeThreshold=timeThreshold.toFixed(2);
				}
				$("#timeThreshold").val(timeThreshold);//连续驾驶时间
				
				var earlyWarningThreshold=(data.earlyWarningThreshold)/60;
				
				   var cnum=earlyWarningThreshold.toString().indexOf(".")
					if(cnum>0){
						earlyWarningThreshold=earlyWarningThreshold.toFixed(2);
				}
				$("#earlyWarningThreshold").val(earlyWarningThreshold);//疲劳预警值
				
				
				var remindInterval=(data.remindInterval)/60;
				
				   var dnum=remindInterval.toString().indexOf(".")
					if(dnum>0){
						remindInterval=remindInterval.toFixed(2);
				}
				$("#remindInterval").val(remindInterval);//提醒间隔
				
				
				$("#remindType").val(data.remindType);//提醒方式
				$("#commandContent").val(data.commandContent);//疲劳播报内容
				$("#earlyCmdContent").val(data.earlyCmdContent);//预报警播报内容
				$("#analysegroupId").val(data.analyseGroupsID);//预报警播报内容
				$("#chooseVehicle").attr("checked","checked");
				var choose = document.getElementById("vehicleTab");
				choose.style.display= "block";
				//$("#coverageGroupsName option:selected").val(data.coverageGroupsID);
				if(data.remindType==2){
					$("#remindType2").attr("checked","checked");
				}else if(data.remindType==3){
					$("#remindType3").attr("checked","checked");
				}else{
					$("#remindType4").attr("checked","checked");
				}
				if(data.is_weixin_notify==1){
					$("#wechart").attr("checked","checked");
				}else{
					$("#wechart").attr("checked",false);
				}
				if(data.is_sms_notify==1){
					$("#notify").attr("checked","checked");
				}else{
					$("#notify").attr("checked",false);
				}
				
			}
			

		});
		
	},
	
	saveVehicleTired : function() {
		var action = 'update';
		var param = {};
		var periodValue;
		
		var require = function(param){
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/vehicleTired/saveVehicleTired.html",
				cache : false,
				data : param,
				dataType : "JSON",
				success : function(data) {
					$("#annal").flexReload();
					/*for (var i = 0; i < 4; i++) {
						if ($(".radio-warp").find("input")[i].checked) {
							$(".radio-warp").find("input")[i].checked=false;
						}
					}*/

					 $(".resultCarCont li div").text("");
					 $("#name").val("");//分析组名
					 
					 $("#continuousRestTime").val("");//休息时间
					 $("#timeThreshold").val("");//连续驾驶时间
					 $("#earlyWarningThreshold").val("");//疲劳预警值
					 $("#remindInterval").val("");//提醒间隔
					 $("#remindType").val("checked","checked");//提醒间隔
					 $("#commandContent").val("");//播报内容
					 $("#earlyCmdContent").val("");
					 $("#allVehicle").attr("checked","checked");
					 $(".resultCarCont li").remove();
					 global.ajaxLoading.pop(data.msg,false,2000);
				}

			});
		};
		
		$("#saveVehicleTiredBtn").click(function() {
			
			/*for (var i = 0; i < 4; i++) {
				if ($(".radio-warp").find("input")[i].checked) {
					periodValue = $($(".radio-warp").find("input")[i])
							.parent("label").text();
				}
			}*/

			var ids = $(".resultCarCont li div").text();
			if(!ids){
				ids=conditionSetting.allVehicleId;
			}
			if($("#allVehicle").attr("checked")=="checked"){
				ids=conditionSetting.allVehicleId;//点击全部车辆
			}
			var name = $.trim($("#name").val());//分析组名
			if(!name){
				$("#content").append("  <p>请填写条件名称</p> ")
				$("#velidateModal").modal("show");
				return;
			}
			var continuousRestTime = $("#continuousRestTime").val();//休息时间
			
			var timeThreshold = $("#timeThreshold").val();//连续驾驶时间
			var earlyWarningThreshold = $("#earlyWarningThreshold").val();//疲劳预警值
			
			var remindInterval = $("#remindInterval").val();//提醒间隔
			
			if(!remindInterval){
				remindInterval=600;
			}else{
				remindInterval=(remindInterval*60).toFixed(0);
			}
			
			var commandContent = $("#commandContent").val();//播报内容
			var earlyCmdContent = $("#earlyCmdContent").val();//预报警播报内容
			var analyseGroupsID = $("#analysegroupId").val();//分析组ID
			
			var remark = $("#remark").val();//备注
			/*if(!remindInterval){
				remindInterval = 10;
			}*/
			
			if(!continuousRestTime){
				continuousRestTime = 1200;
			}else{
				continuousRestTime = (continuousRestTime*60).toFixed(0);
			}
			
			if(!timeThreshold){
				timeThreshold = 14400;
			}else{
				timeThreshold = (timeThreshold*60).toFixed(0);
			}
			if(!earlyWarningThreshold){
				earlyWarningThreshold = 12600;
			}else{
				earlyWarningThreshold = (earlyWarningThreshold*60).toFixed(0);
			}
			if(!commandContent){
				commandContent = "您已经疲劳驾驶，请注意休息";
			}
			if(!earlyCmdContent){
				earlyCmdContent = "您已经连续开车很长时间，请注意休息";
			}
			var remindType=2;//提醒方式
			if($("#remindType3").attr("checked")=="checked"){
				remindType=3;
			}
			if($("#remindType4").attr("checked")=="checked"){
				remindType=4;
			}
			var wechart = 0;//通知方式
			if($("#wechart").attr("checked")=="checked"){
				 wechart=1
			}
			var notify = 0;
			if($("#notify").attr("checked")=="checked"){
				notify=1
			}
			param =  {
				"Name" : name,
				"EarlyWarningThreshold" : earlyWarningThreshold,
				"Remark" : remark,
				"ids" : ids,
				"ContinuousRestTime" : continuousRestTime,
				"TimeThreshold" : timeThreshold,
				"RemindInterval" : remindInterval,
				"CommandContent" : commandContent,
				"RemindType":remindType,
				"EarlyCmdContent" : earlyCmdContent,
				"AnalyseGroupsID" : analyseGroupsID,
				"Is_weixin_notify" : wechart,
				"Is_sms_notify" : notify,
				"ID":conditionSetting.alarmId
			}
			func();
			conditionSetting.queryTabInit.reflash(0);
		});
		
		var func=function(){
			if(!param.ID){
				action="insert";
			}
			param.action = action;
			require(param);
		};
		
      return{
		updateVehicleTired : function(action){
			func(action);
		}
     }
	},
	/**
	 * 删除疲劳设置
	 */

	deleteVehicleTired : function(id,analyseGroupsID) {
		$("#comDelBtn").click(function(){
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/vehicleTired/deleteVehicleTired.html",
				cache : false,
				data : {
					"id" : id,
					"AnalyseGroupsID" : analyseGroupsID
				},
				dataType : "JSON",
				success : function(data) {
					$("#myModalDel").modal("hide");
					
				}

			});
			$.ajax({
				type : 'post',
				async : true,
				url : appCtx + "reportset/vehicleTired/deleteVehicleTiredVehicle.html",
				cache : false,
				data : {
					"AnalyseGroupsID" : analyseGroupsID
				},
				dataType : "JSON",
				success : function(data) {
					$("#myModalDel").modal("hide");
					
				}

			});
			$("#annal").flexReload();
		})
		

	},
	queryVehicle : function(id) {

		$('#myModal').on('hidden', function() {
			$(".ownCarCont li").remove();
		})
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/queryVehicle.html",
			cache : false,
			data : {
				"id" : id
			},
			dataType : "JSON",
			success : function(data) {
				var vehicleArr=data.rows;
				var size=vehicleArr.length;
				for (var i = 0; i < size; i++) {
						$(".ownCarCont").append(
								'<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ vehicleArr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ vehicleArr[i].vehicleId + ',</div></li>')
				}
			}

		});
		
		
		
	},
	annal : function() {
		/**
		 * 操作列
		 */
		function formmatOperation(iD,analyseGroupsID) {
			var handleStr = "";

			handleStr = '<a href="javascript:void(0)" onclick="conditionSetting.updateVehicleSpeed('
					+ iD+','+analyseGroupsID
					+ ')" title="" ><span style='+'"color: blue;'+'">修改</span></a>&nbsp;&nbsp;<a href="#myModalDel" onclick="conditionSetting.deleteVehicleTired('
					+ iD +','+analyseGroupsID + ')" '+'data-toggle="'+'modal"'+'title="'+'" ><span style='+'"color: blue;'+'">删除</span></a>';

			return handleStr;
		}
		;
		function formmatVehicle(analyseGroupsID) {
			var handleStr = "";
			handleStr = '<a href="' + '#myModal' + '"  role=' + '"button'
					+ '" onclick=' + '"conditionSetting.queryVehicle('
					+ analyseGroupsID + ')" data-toggle=' + '"modal'
					+ '"><span style=' + '"color: blue;' + '"</span>查看车辆</a>';
			return handleStr;
		};
		
		function notifySelect(param) {
			if(param = 1){
				return "是";
			}else{
				return "否";
			}
		};

		$("#annal").flexigrid({
			dataType : 'json',
			url : appCtx + 'reportset/vehicleTired/queryAllVehicleTired.html',
			params : [],
			contentType : "application/json; charset=utf-8",
			method : 'POST',
			widthType:'%',
	        width:$('.tab-alarm-cont').width(),
			colModel : [ {
				display : '条件名称',
				name : 'name',
				width : 7,
				sortable : false,
				// hide:true,
				align : 'left',
				checked : 'checked',
					paramcolnames : [ 'name'],
					handlefunction : function(name) {
								return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+name+'" >'+name+'</a>';
							}
			}, {
				display : '疲劳值(分钟)',
				name : 'timeThreshold',
				width : 7,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'timeThreshold' ],
				handlefunction : function(timeThreshold) {
					if(timeThreshold){
						timeThreshold=timeThreshold/60;
						   var num=timeThreshold.toString().indexOf(".")
						if(num>0){
							timeThreshold=timeThreshold.toFixed(2);
					}
				}
					return timeThreshold;
				}
			}, {
				display : '疲劳预警值',
				name : 'earlyWarningThreshold',
				width : 7,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'earlyWarningThreshold' ],
				handlefunction : function(earlyWarningThreshold) {
					if(earlyWarningThreshold){
						earlyWarningThreshold=earlyWarningThreshold/60;
						   var num=earlyWarningThreshold.toString().indexOf(".")
						if(num>0){
							earlyWarningThreshold=earlyWarningThreshold.toFixed(2);
					}
				}
					return earlyWarningThreshold;
				}
			}/*, {
				display : '周期值',
				name : 'periodValue',
				width : 5,
				sortable : false,
				align : 'center',
			}*/, {
				display : '分析时间',
				name : 'startTime',
				width : 11,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'startTime', 'endTime' ],
				handlefunction : function(startTime, endTime) {
					return startTime + "至" + endTime;
				}
			}, {
				display : '监控车辆',
				name : 'Handler',
				width : 6,
				sortable : false,
				align : 'center',

				paramcolnames : [ 'analyseGroupsID' ],
				handlefunction : formmatVehicle,
			}, {
				display : '休息时间',
				name : 'continuousRestTime',
				width : 7,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'continuousRestTime' ],
				handlefunction : function(continuousRestTime) {
					if(continuousRestTime){
						continuousRestTime=continuousRestTime/60;
						   var num=continuousRestTime.toString().indexOf(".")
						if(num>0){
							continuousRestTime=continuousRestTime.toFixed(2);
					}
				}
					return continuousRestTime;
				}
			},  {
				display : '报警播报内容',
				name : 'commandContent',
				width : 8,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'commandContent'],
				handlefunction : function(commandContent) {
							return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+commandContent+'" >'+commandContent+'</a>';
						}
			}
			,  {
				display : '预警播报内容',
				name : 'earlyCmdContent',
				width : 8,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'earlyCmdContent'],
				handlefunction : function(earlyCmdContent) {
							return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+earlyCmdContent+'" >'+earlyCmdContent+'</a>';
						}
			},{
				display : '提醒方式',
				name : 'remindType',
				width : 10,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'remindType' ],
				handlefunction : function(remindType) {
					if(remindType==2){
						remindType="平台报警";
					}else if(remindType==3){
						remindType="TTS语音播报"
					}else{
						remindType="平台/TTS语音播报"
					}
					return remindType;
				}
			},  {
				display : '微信通知',
				name : 'is_weixin_notify',
				width : 6,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_weixin_notify' ],
				handlefunction : function(is_weixin_notify){
					if(is_weixin_notify==1){
						is_weixin_notify="是";
					}else{
						is_weixin_notify="否";
					}
					return is_weixin_notify;
				}
			}
			,  {
				display : '短信通知',
				name : 'Is_sms_notify',
				width : 6,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_sms_notify' ],
				handlefunction :function(is_sms_notify){
					if(is_sms_notify==1){
						is_sms_notify="是";
					}else{
						is_sms_notify="否";
					}
					return is_sms_notify;
				}
			}, {
				display : '备注',
				name : 'remark',
				width : 7,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'remark'],
				handlefunction : function(remark) {
							return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+remark+'" >'+remark+'</a>';
						}
			},{
				display : '操作',
				name : 'operation',
				width : 5,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'iD','analyseGroupsID' ],
				handlefunction : formmatOperation
			} ],
			

			resizable : false,
			sortname : "terminalId",// 第一次加载数据时排序列
			sortorder : "desc",// 第一次加载数据时排序类型
			usepager : false,// 是否分页，默认为true。
			showToggleBtn : false,
			showTableToggleBtn : false,// 是否显示收起/打开按钮,默认不显示。
			// useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
			// rp : 10,// 每页记录数，默认为10
			// rpOptions: [10,50,100,300,500], // 每页结果数
			checkbox : true,// 是否要多选框,默认为false。
			rowId : 'terminalId',// 多选框绑定行的id,只有checkbox :
			// true时才有效。
			singleSelect : true,
			height : nav.whiceH[1] - 130, // 表格高度
			titDisplay : false,
			onSuccess : function(data) {
			},
			onSelectRow : function(row) {

			},
			onSelectAllRow : function(data) {

			}

		});
	}
};

conditionSetting.init();